package com.tf_cut.backend.person.mobile;

import com.tf_cut.backend.common.pojo.Result;
import com.tf_cut.backend.person.service.PersonBankService;
import com.tf_cut.backend.person.service.PersonBillService;
import com.tf_cut.backend.person.service.PersonService;
import com.tf_cut.backend.person.vo.PersonAndAccountVo;
import com.tf_cut.backend.person.vo.PersonBankVo;
import com.tf_cut.backend.person.vo.PersonBillAndConsigneeVo;
import com.tf_cut.backend.util.SendMail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/backend/person/mobile")
public class PersonMobileController {

    @Autowired
    private PersonService personService;
    @Autowired
    private PersonBillService personBillService;
    @Autowired
    private PersonBankService personBankService;
    @Autowired
    private SendMail sendMail;


    @GetMapping("/person/get")
    public Result<PersonAndAccountVo> get(@RequestParam  String loginId) {
        List<PersonAndAccountVo> data = this.personService.list(PersonAndAccountVo.builder().loginId(loginId).build()).getData();
        if(data==null||data.isEmpty()){
            return Result.of(null,false,"数据错误");
        }else {
            return Result.of(data.get(0));
        }
    }

    @PostMapping("/person/insert")
    public Result<PersonAndAccountVo> insertPersonAndAccount(@RequestBody  PersonAndAccountVo  avo) {
        Result<PersonAndAccountVo> result = null;
        try {
        	//注册会调用这个方法，如果是注册进入方法，那么状态必定是N
        	if(!avo.getCardStatus().equals("X")){
        		avo.setCardStatus("N");        		
        	}
            result = personService.insertPersonAndAccount(avo);
        } catch(Exception e) {
            log.error(e.getMessage(),e);
            result = Result.of(avo);
            result.setMsg(e.getMessage());
            result.setFlag(false);
        }
        return result;
    }

    @PostMapping("/person/updateInfo")
    public Result<PersonAndAccountVo> updateInfo(@RequestBody PersonAndAccountVo svo) {
        Result<PersonAndAccountVo> result;
        try {
        	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            log.info("person updateInfo,reqParam={}",svo);

            //需要检验身份证和手机号
            if(null!=svo.getIsPersonAuth()&&svo.getIsPersonAuth().intValue()==1){

                String cardCode = svo.getCardCode();
                String mobile = svo.getMobile();

                List<PersonAndAccountVo> data = this.personService.list(PersonAndAccountVo.builder().mobile(mobile).build()).getData();
                if(!data.isEmpty()){
                    List<PersonAndAccountVo> collect = data.stream().filter(e -> !e.getPersonId().equals(svo.getPersonId())).collect(Collectors.toList());
                    if(collect.size()>0){
                        result = Result.of(svo);
                        result.setMsg("手机号重复");
                        result.setFlag(false);
                    }
                }
                List<PersonAndAccountVo> data1 = this.personService.list(PersonAndAccountVo.builder().cardCode(cardCode).build()).getData();
                if(!data1.isEmpty()){
                    List<PersonAndAccountVo> collect1 = data1.stream().filter(e -> !e.getPersonId().equals(svo.getPersonId())).collect(Collectors.toList());
                    if(collect1.size()>0){
                        result = Result.of(svo);
                        result.setMsg("身份证号重复");
                        result.setFlag(false);
                    }
                }

            }
            if(svo.getCardStatus() != null && svo.getCardStatus().equals("X")){
            	svo.setUpdateTime(new Date());
            }

            result = personService.save(svo);
            
            if(svo.getPersonName() != null && svo.getCardCode() != null && svo.getMobile() !=null){
            	sendMail.registerInfoMailSend(svo);
            }
        } catch(Exception e) {
            log.error("person updateInfo error:{}",e.getMessage(),e);
            result = Result.of(svo);
            result.setMsg(e.getMessage());
            result.setFlag(false);
        }
        return result;
    }

    @PostMapping("/bill/insert")
    public Result<PersonBillAndConsigneeVo> insertBill(@RequestBody PersonBillAndConsigneeVo svo) {
        svo.setCreateTime(new Date());
        //0：待确认；1：已确认；2：已驳回
        svo.setStatus(0);
        Result<PersonBillAndConsigneeVo> result = personBillService.insertBillAndConsignee(svo);
        return result;
    }

    @PostMapping("/bank/insert")
    public Result<PersonBankVo> insertPersonBank(@RequestBody PersonBankVo svo) {
        Result<PersonBankVo> result = personBankService.insertBank(svo);
        return result;
    }

    @GetMapping("/bank/get")
    public Result<PersonBankVo> getBank(@RequestParam  String personId) {
        List<PersonBankVo> data = this.personBankService.list(PersonBankVo.builder().personId(personId).build()).getData();
        return Result.of(data.isEmpty() ? new PersonBankVo() : data.get(0));
    }

}
